﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.MyLinq;
using Huirui.Cavan.Core.Persistent;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Domain.MainBoundedContext.SystemModule
{
    public class CustomerSpecification : ISpecification<Customer, CustomerSearch>
    {
        #region ISpecification<Customer,CustomerSearch> 成员

        public CustomerSearch SearchParams
        {
            get;
            set;
        }

        #endregion

        #region ISpecification<Customer> 成员

        public System.Linq.Expressions.Expression<Func<Customer, bool>> Expression
        {
            get
            {
                IQueryBuilder<Customer> queryBuilder = QueryBuilder.Create<Customer>();

                if (null != SearchParams)
                {
                    if (SearchParams.CorporationCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(c => c.Corporation.Id, SearchParams.CorporationCode);
                    }
                    if (SearchParams.ID.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(c => c.Id, SearchParams.ID);
                    }
                    if (SearchParams.Name.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(c => c.Name, SearchParams.Name);
                    }
                    if (SearchParams.Contact.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(c => c.Contact, SearchParams.Contact);
                    }
                    if (SearchParams.AgentTypeID > 0)
                    {
                        queryBuilder = queryBuilder.Equals(c => (int)c.AgentType, SearchParams.AgentTypeID);
                    }
                    if (SearchParams.AreaCode.IsNotEmpty() && !SearchParams.AreaCode.Equals("0"))
                    {
                        queryBuilder = queryBuilder.Equals(s => s.Area.Id, SearchParams.AreaCode);
                    }
                    queryBuilder = queryBuilder.Equals(s => s.Status, SearchParams.Status);
                }

                return queryBuilder.Expression.Expand();
            }
        }

        #endregion
    }
}
